﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CNotebook
{
    public class ORManager:ConnectionManager
    {
        public void AddOR(ORUnit oUnit)
        {
            SqlCommand oCommand = new SqlCommand();
            oCommand.Connection = this.Connection;

            oCommand.CommandText = "INSERT INTO ORCPT (DocNum, ORPRNo, CardCode, CardName, Amount, DocDate, DateCreated, CreatedBy, DateModified, ModifiedBy, Status) VALUES (@DocNum, @ORPRNo, @CardCode, @CardName, @Amount, @DocDate, @DateCreated, @CreatedBy, @DateModified, @ModifiedBy, @Status)";
            oCommand.Parameters.Add(new SqlParameter("@DocNum", oUnit.DocNum));
            oCommand.Parameters.Add(new SqlParameter("@ORPRNo", oUnit.ORPRNo));
            oCommand.Parameters.Add(new SqlParameter("@CardCode", oUnit.CardCode));
            oCommand.Parameters.Add(new SqlParameter("@CardName", oUnit.CardName));
            oCommand.Parameters.Add(new SqlParameter("@Amount", oUnit.Amount));
            oCommand.Parameters.Add(new SqlParameter("@DocDate", oUnit.DocDate));
            oCommand.Parameters.Add(new SqlParameter("@DateCreated", oUnit.DateCreated));
            oCommand.Parameters.Add(new SqlParameter("@CreatedBy", oUnit.CreatedBy));
            oCommand.Parameters.Add(new SqlParameter("@DateModified", oUnit.DateModified));
            oCommand.Parameters.Add(new SqlParameter("@ModifiedBy", oUnit.ModifiedBy));
            oCommand.Parameters.Add(new SqlParameter("@Status", oUnit.Status));

            oCommand.ExecuteNonQuery();


            foreach (DataRow _row in oUnit.AppliedInvoices.Rows)
            {

                oCommand = new SqlCommand();
                oCommand.Connection = this.Connection;

                oCommand.CommandText = "INSERT INTO OR_SIDR_APPLIED (DocNum, ORPRNo, AR_SIDRNo, Amount, AppliedAmount) VALUES (@DocNum, @ORPRNo, @AR_SIDRNo, @Amount, @AppliedAmount)";
                oCommand.Parameters.Add(new SqlParameter("@DocNum", oUnit.DocNum));
                oCommand.Parameters.Add(new SqlParameter("@ORPRNo", oUnit.ORPRNo));
                oCommand.Parameters.Add(new SqlParameter("@AR_SIDRNo", _row["AR_SIDRNo"].ToString()));
                oCommand.Parameters.Add(new SqlParameter("@Amount", _row["Amount"].ToString()));
                oCommand.Parameters.Add(new SqlParameter("@AppliedAmount", _row["AppliedAmount"].ToString()));

                oCommand.ExecuteNonQuery();
            }


            foreach (DataRow _row in oUnit.AppliedExpenses.Rows)
            {

                oCommand = new SqlCommand();
                oCommand.Connection = this.Connection;

                oCommand.CommandText = "INSERT INTO OR_SIDR_EXPENSES (DocNum, ORPRNo, AR_SIDRNo, ExpenseCode, ExpenseName, Amount) VALUES (@DocNum, @ORPRNo, @AR_SIDRNo, @ExpenseCode, @ExpenseName, @Amount)";
                oCommand.Parameters.Add(new SqlParameter("@DocNum", oUnit.DocNum));
                oCommand.Parameters.Add(new SqlParameter("@ORPRNo", oUnit.ORPRNo));
                oCommand.Parameters.Add(new SqlParameter("@AR_SIDRNo", _row["AR_SIDRNo"].ToString()));
                oCommand.Parameters.Add(new SqlParameter("@ExpenseCode", _row["ExpenseCode"].ToString()));
                oCommand.Parameters.Add(new SqlParameter("@ExpenseName", _row["ExpenseName"].ToString()));
                oCommand.Parameters.Add(new SqlParameter("@Amount", _row["Amount"].ToString()));

                oCommand.ExecuteNonQuery();
            }
        }
    }
}
